package com.qire.manhua.model.viewModel.impl;

import com.qire.antsbinder.viewModel.ViewModelFactory;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.RoomCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.utils.ThemeUtil;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.activity.HomeActivity;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.view.BottomNavigationView;

public class HomeViewModel extends BaseViewModel implements IViewModel {

    /** 书架 Fragment 的 viewModel */
    private BookshelfViewModel bookshelfViewModel;
    /** 推荐 Fragment 的 viewModel */
    private RecommendViewModel recommendViewModel;
    /** 分类 Fragment 的 viewModel */
    private ClassificationViewModel classificationViewModel;
    /** 用户中心 Fragment 的 viewModel */
    private UserCenterViewModel userCenterViewModel;

    @Override
    public int getVariableId() {
        return BR.homeViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(HomeActivity activity) {
        bookshelfViewModel      = ViewModelFactory.crate(activity, BookshelfViewModel.class);
        recommendViewModel      = ViewModelFactory.crate(activity, RecommendViewModel.class);
        classificationViewModel = ViewModelFactory.crate(activity, ClassificationViewModel.class);
        userCenterViewModel     = ViewModelFactory.crate(activity, UserCenterViewModel.class);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
    }
    //endregion #附加管理 end

    /**
     * 跳转到索引指定的选项卡页
     * @param activity 所属页面
     * @param tabIndex 选项卡索引，如果选项卡为 {@link PageTabIndex#TAB_INDEX_INVALID TAB_INDEX_INVALID} 无效索引则跳转行为被丢弃，不会发生任何变化
     */
    public void gotoTabPage(HomeActivity activity, int tabIndex) {
        gotoTabPage(activity, tabIndex, PageTabIndex.TAB_INDEX_INVALID);
    }

    /**
     * 跳转到指定索引的选项卡页，并转向指定的2级选项卡页
     * @param activity 所属页面
     * @param tabIndex 选项卡索引，如果选项卡为 {@link PageTabIndex#TAB_INDEX_INVALID TAB_INDEX_INVALID} 无效索引则跳转行为被丢弃，不会发生任何变化
     * @param subTabIndex 二级选项卡页，如果二级选项卡页为 {@link PageTabIndex#TAB_INDEX_INVALID TAB_INDEX_INVALID} 无效索引则只会转到一级选项卡，二级选项卡不会发生改变
     */
    public void gotoTabPage(HomeActivity activity, int tabIndex, int subTabIndex) {
        if (tabIndex == PageTabIndex.TAB_INDEX_INVALID) {
            return;
        }

        boolean dark;
        String tabPath;
        BaseViewModel roomViewModel;

        switch (tabIndex) {
            case PageTabIndex.TAB_INDEX_HOME_BOOKSHELF :
                dark = true;
                tabPath = Navigation.Fragment.BookshelfFragment;
                roomViewModel = bookshelfViewModel;
                break;
            case PageTabIndex.TAB_INDEX_HOME_CLASSIFICATION :
                dark = true;
                tabPath = Navigation.Fragment.ClassificationFragment;
                roomViewModel = classificationViewModel;
                break;
            case PageTabIndex.TAB_INDEX_HOME_USER_CENTER :
                dark = true;
                tabPath = Navigation.Fragment.UserFragment;
                roomViewModel = userCenterViewModel;
                break;
            case PageTabIndex.TAB_INDEX_HOME_RECOMMEND :
            default :
                dark = false;
                tabPath = Navigation.Fragment.RecommendFragment;
                roomViewModel = recommendViewModel;
        }

        ThemeUtil.changeStatusBarLightOrDark(activity, dark, true);
        RoomCard roomCard = AntsRouter.ROUTER.buildCard(tabPath);
        roomCard.withData("viewModel", roomViewModel);
        if(subTabIndex != PageTabIndex.TAB_INDEX_INVALID) {
            roomCard.withData("pageNo", subTabIndex);
        }
        roomCard.to(activity, R.id.app_home_content);
    }

    /**
     * 刷新底部导航菜单UI状态
     * @param homeBottomNavMenu 底部导航菜单
     * @param tabIndex 按钮索引
     */
    public void refreshBottomNavMenuUI(BottomNavigationView homeBottomNavMenu, int tabIndex) {
        if(ObjectUtil.nonNull(homeBottomNavMenu)) {
            homeBottomNavMenu.getMenu().getItem(tabIndex).setChecked(true);
        }
    }

}
